package com.tim.third.demo;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooKeeperMain;

import java.net.URLDecoder;
import java.util.List;

/**
 * zookeeper
 */
public class ZookeeperDemo {
    ZooKeeperMain zooKeeperMain;// zk命令行客户端

    public static void main(String[] args) throws Exception {
        getProviders("");
    }

    public static void getProviders(String classFullName) throws Exception {
        String hostPort =" HOST PORT";
        ZooKeeper zooKeeper=null;
        try{
            zooKeeper = new ZooKeeper( hostPort, 1000 * 10, System.out::println);
            // 获取providers子节点
            String parentName = String.format("/dubbo/%s/providers", classFullName);
            List<String> children = zooKeeper.getChildren(parentName, false);
            if(CollectionUtils.isNotEmpty(children)){
                String provider = children.get(0);
                provider = URLDecoder.decode(provider);
                System.out.println(provider);
            }
        }finally{
            if(zooKeeper!=null){
                zooKeeper.close();
            }
        }
    }


}
